package manage.actions.action;

import manage.actions.ManageAction;
import manage.commons.annontations.RequiredLogin;
import manage.commons.annontations.SuperManager;
import manage.commons.utils.InputStreamUtil;
import manage.commons.utils.Pager;
import manage.modules.action.model.ActionRange;
import manage.modules.action.service.ActionRangeService;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

@RequiredLogin
@SuperManager
public class ActionRangeAction extends ManageAction {

	private static final long serialVersionUID = 287325083366829505L;

	private ActionRangeService actionRangeService;
	private Pager<ActionRange> pager = new Pager<ActionRange>(200);
	private ActionRange actionRange;

	@Override
	@Action("action-range-input")
	public String input() {
		return SUCCESS;
	}

	@Override
	@Action(value = "action-range-addorupdate", results = { @Result(name = SUCCESS, location = "/action/action-range-form.jsp") })
	public String forwardAddOrUpdatePage() {
		if (isAddOperater()) {
			action = "/action/action-range-add.do";
			getRequest().setAttribute("btnKeyWord", "添加数据范围");
		} else if (isUpdateOperater()) {
			action = "/action/action-range-edit.do";
			getRequest().setAttribute("btnKeyWord", "更新数据范围");
			actionRange = actionRangeService.select(actionRange.getRangeId());
		}
		return SUCCESS;
	}

	@Action(value = "action-range-query", results = { @Result(name = SUCCESS, location = "/action/action-range-list.jsp") })
	public String query() throws Exception {
		if (actionRange != null) {
			actionRangeService.query(actionRange, pager);
			pager.pagerUtils(pager, getRequest());
		} else {
			pager.setList(actionRangeService.listAll());
			pager.setRowCount(actionRangeService.getCountOfAll());
			pager.pagerUtils(pager, getRequest());
		}
		return SUCCESS;
	}

	@Action(value = "action-range-select", results = { @Result(name = SUCCESS, type = "json", params = {
			"root", "actionRange" }) })
	public String select() {
		if (null != actionRange && null != actionRange.getRangeId()) {
			boolean exists = actionRangeService
					.exists(actionRange.getRangeId());
			if (exists) {
				actionRange = actionRangeService.select(actionRange
						.getRangeId());
			}
		}
		return SUCCESS;
	}

	@Action(value = "action-range-add", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String add() throws Exception {
		if (null != actionRange && actionRange.getRangeName() != null) {
			int count = actionRangeService.insert(actionRange);
			if (count == 1) {
				inputStream = InputStreamUtil.getSuccessInputStream();
			} else {
				inputStream = InputStreamUtil.getFailedInputStream();
			}
		}
		return SUCCESS;
	}

	@Action(value = "action-range-delete", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String delete() throws Exception {
		if (null != actionRange && null != actionRange.getRangeId()) {
			boolean exists = actionRangeService
					.exists(actionRange.getRangeId());
			if (exists) {
				int delNum = actionRangeService
						.delete(actionRange.getRangeId());
				if (delNum == 1) {
					inputStream = InputStreamUtil.getSuccessInputStream();
				}
			} else {
				inputStream = InputStreamUtil.getNotExistsInputStream();
			}
		} else {
			inputStream = InputStreamUtil.getFailedInputStream();
		}
		return SUCCESS;
	}

	@Action(value = "action-range-edit", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String edit() throws Exception {
		if (null != actionRange && null != actionRange.getRangeId()
				&& actionRange.getRangeName() != null) {
			boolean exists = actionRangeService
					.exists(actionRange.getRangeId());
			if (exists) {
				int count = actionRangeService.update(actionRange);
				if (count >= 1) {
					inputStream = InputStreamUtil.getSuccessInputStream();
				} else {
					inputStream = InputStreamUtil.getNoChangeInputStream();
				}
			} else {
				inputStream = InputStreamUtil.getNotExistsInputStream();
			}
		}
		return SUCCESS;
	}

	public Pager<ActionRange> getPager() {
		return pager;
	}

	public void setPager(Pager<ActionRange> pager) {
		this.pager = pager;
	}

	public ActionRange getActionRange() {
		return actionRange;
	}

	public void setActionRange(ActionRange actionRange) {
		this.actionRange = actionRange;
	}

	public void setActionRangeService(ActionRangeService actionRangeService) {
		this.actionRangeService = actionRangeService;
	}

}
